<!-- 夜巡管理 -->
<template>
  <div>
    <el-card style="width: 98%; margin: 0 auto; margin-top: 10px">
      <el-form ref="ruleFormRef" :model="form" label-width="auto" :inline="true">
        <el-form-item label="上报人:" prop="name">
          <el-input v-model="form.name" clearable />
        </el-form-item>
        <el-form-item label="巡检地址:" prop="address">
          <el-select v-model="form.address" placeholder="请选择" clearable style="width: 220px">
            <el-option v-for="item in list" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="巡查上报时间:" prop="beginDate">
          <el-date-picker
            v-model="value"
            type="daterange"
            clearable
            unlink-panels
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="datachange"
          />
        </el-form-item>
        <el-form-item label="巡检状态:" prop="state">
          <el-select v-model="form.state" placeholder="请选择" clearable style="width: 220px">
            <el-option label="正常" value="正常" />
            <el-option label="异常" value="异常" />
            <el-option label="未巡查" value="未巡查" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search">查询</el-button>
          <el-button :icon="Refresh" @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="width: 98%; margin: 0 auto; margin-top: 10px">
      <ELTable v-bind="tableConfig" ref="nightTable">
        <template #btns="{ deleteAll }">
          <el-button type="success" @click="add">
            <el-icon class="icon">
              <Plus />
            </el-icon>
            增加
          </el-button>
          <el-button @click="skipAddress">
            <el-icon class="icon">
              <Location />
            </el-icon>
            地址管理
          </el-button>
          <el-button type="danger" @click="deleteAll">
            <el-icon class="icon">
              <Delete />
            </el-icon>
            批量删除
          </el-button>
        </template>

        <template #addTime="{ scope }">
          {{ dayjs(scope.row.addTime).format("YYYY-MM-DD HH:mm") }}
        </template>

        <template #operate="{ scope, deleteHandle }">
          <el-button
            :icon="Delete"
            text
            size="small"
            class="btn"
            @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </ELTable>
    </el-card>

    <!-- 添加 -->
    <nightDialog v-if="dialogVisible" @close="handelClose" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { Search, Refresh, Delete, Location } from "@element-plus/icons-vue";
import { patrolDel, patrolDelAll, patrolList } from "@/api/care/night/night";
import { nightParams } from "@/api/care/night/type";
import ELTable, { TableData } from "@/components/ELTable.vue";
import dayjs from "dayjs";
import nightDialog from "@/components/care/nightAdd.vue";
import { addressList } from "@/api/care/address/address";
import { addressListData } from "@/api/care/address/type";
import { useRouter } from "vue-router";
import { FormInstance } from "element-plus";
const router = useRouter();

const dialogVisible = ref(false);

const value = ref([]);
const form = reactive<nightParams>({
  page: 1,
  pageSize: 10,
  name: "",
  address: "",
  state: "",
  beginDate: "",
  endDate: "",
});

// 地址列表
const list = ref<addressListData[]>([]);
const getAddressList = async () => {
  let res = await addressList({ page: 1, pageSize: 10000 });
  // console.log('地址', res);
  list.value = res.list;
};

// 跳转地址管理
const skipAddress = () => {
  router.push("/care/address");
};

// 添加
const add = () => {
  dialogVisible.value = true;
};

// 搜索
const search = () => {
  form.page = 1;
  nightTable.value.getData(form);
};

// 重置
const ruleFormRef = ref<FormInstance>();
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  value.value = [];
};

// 日期
const datachange = (e: any) => {
  // console.log(e);
  if (e != null) {
    form.beginDate = e[0];
    form.endDate = e[1];
  } else {
    form.beginDate = "";
    form.endDate = "";
  }
};

// 关闭弹框
const nightTable = ref();
const handelClose = () => {
  dialogVisible.value = false;
  nightTable.value.getData();
};

// 表格
const tableConfig = reactive<TableData>({
  loadData: patrolList,
  delete: patrolDel,
  deleteAll: patrolDelAll,
  columns: [
    {
      prop: "id",
      label: "序号",
      attrs: {
        width: "70px",
      },
    },
    {
      prop: "address",
      label: "巡检地点",
    },
    {
      prop: "addTime",
      label: "巡检上报时间",
      slot: true,
    },
    {
      prop: "staffName",
      label: "上报人",
    },
    {
      prop: "state",
      label: "巡检状态",
    },
    {
      prop: "comment",
      label: "巡查记录",
    },
    {
      prop: "operate",
      label: "操作",
      slot: true,
    },
  ],
});

onMounted(() => {
  getAddressList();
});
</script>

<style lang="less" scoped>
.btn {
  color: rgb(245, 108, 127);
}
</style>
